import { Component, OnInit, ViewChild } from '@angular/core';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { NzMessageService } from 'ng-zorro-antd/message';
import { _HttpClient } from '@delon/theme';
import { SFSchema, SFUISchema } from '@delon/form';
import { environment } from '@env/environment';

@Component({
  selector: 'app-sys-tenant-edit',
  templateUrl: './edit.component.html',
})
export class SysTenantEditComponent implements OnInit {
  record: any = {};
  i: any;
  schema: SFSchema = {
    properties: {
      code: { type: 'string', title: '代码' },
      name: { type: 'string', title: '名称', maxLength: 30 },
      sort: { type: 'integer', title: '排序' },
      enabled: {
        type: 'string',
        title: '是否有效',
        enum: [
          { label: '是', value: 1 },
          { label: '否', value: 0 },
        ],
        default: 1,
      },
      description: { type: 'string', title: '描述', maxLength: 140 },
    },
    required: ['code', 'name'],
  };
  ui: SFUISchema = {
    '*': {
      spanLabelFixed: 100,
      grid: { span: 12 },
    },
    $enabled: {
      widget: 'radio',
      styleType: 'button',
      buttonStyle: 'solid',
    },
    $description: {
      widget: 'textarea',
      grid: { span: 24 },
    },
  };

  constructor(private modal: NzModalRef, private msgSrv: NzMessageService, public http: _HttpClient) {}

  ngOnInit(): void {
    if (this.record.id > 0)
      this.http.get(`${environment.sys.ctx}/sysTenant/${this.record.id}`).subscribe(res => (this.i = res));
  }

  save(value: any) {
    this.http.put(`${environment.sys.ctx}/sysTenant`, value).subscribe(res => {
      this.msgSrv.success('保存成功');
      this.modal.close(true);
    });
  }

  saveAs(value: any) {
    value.id = null;
    this.save(value);
  }

  close() {
    this.modal.destroy();
  }
}
